import storage from 'store'
import { XXTEA } from '@/utils/xxtea.min'
import { ACCESS_TOKEN } from '@/store/mutation-types'

const TokenKey = ACCESS_TOKEN

const ExpiresInKey = 'octH5User-Expires-In'

const version = '1.0'

const KEY = 'Gp#R*Vh@9!V?A/y+=D$U^C*(5%P)K@49^+&3&o*W(rJv*4(e%a/eA#Z1@81%mB^mw*u!u*c-'

const sha256 = require('js-sha256').sha256

export function getToken() {
  return storage.get(TokenKey)
}

export function setToken(token) {
  return storage.set(TokenKey, token)
}

export function removeToken() {
  return storage.remove(TokenKey)
}

export function getExpiresIn() {
  return storage.get(ExpiresInKey)
}

export function setExpiresIn(expiresIn) {
  return storage.set(ExpiresInKey, expiresIn)
}

export function removeExpiresIn() {
  return storage.remove(ExpiresInKey)
}
export function reBuildRequestConfig(config, responseType) {
  let token = getToken()
  token = token ? token : "''"
  const ts = new Date().getTime()
  const uuid = ts.toString(16)
  const m = getRandomNum(0, 5)
  const d = getRamNumber()
  const notice = d.toString(16)
  const sha = sha256(ts + uuid + token + version + notice + m)
  const sign = notice + sha.substring(m, m + 64 - 16 - 1) + m
  const sTime = ts.toString()
  const time = parseInt(sTime.substring(sTime.length - 3, sTime.length))
  const mode1 = (time + parseInt(ts / 1000)) % 5
  const sha1 = sha256(token + KEY + sTime + uuid + mode1)
  const key1 = sha1.substring(mode1, mode1 + 8) + sha1.substring(32 - 8, 32)
  const offset = mode1 + 32
  const key2 = sha1.substring(offset, offset + 8) + sha1.substring(64 - 8, 64)
  const body = XXTEA.encryptToBase64(
    JSON.stringify({
      dev: JSON.stringify({ type: 'web' }),
      data: JSON.stringify(config.data ? config.data : {}),
    }),
    key1
  )
  // const defaultBaseUrl = 'https://b4b4-171-103-21-21.ngrok-free.app/vietnam-travel-admin/rout'
  const defaultBaseUrl = '/vietnam-travel-admin/rout'
  config.headers['Authorization'] = token
  config.headers['ts'] = sTime
  config.headers['v'] = version
  config.headers['sign'] = sign
  config.headers['functionid'] = key1
  config.headers['method'] = XXTEA.encryptToBase64(config.url, key1)
  if (responseType == 'blob') {
    config.url = '/exportapi'
    config.data = body + ''
    config.headers['Content-Type'] = 'application/json'
  } else if (responseType == 'upload') {
    // config.url = '/uploadapi'
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
  } else {
    config.url = defaultBaseUrl
    config.data = body + ''
    config.headers['Content-Type'] = 'application/json'
  }
  config.method = 'post'
  return { key1: key1, key2: key2 }
}

// 获取最小值到最大值之前的整数随机数（不包含最大值）
export function getRandomNum(Min, Max) {
  var Range = Max - Min
  var Rand = Math.random()
  return Min + Math.round(Rand * Range)
}

//JS获取随机的16位十六进制的数
export function getRamNumber() {
  var result = ''
  for (var i = 0; i < 16; i++) {
    result += Math.floor(Math.random() * 16).toString(16) //获取0-15并通过toString转16进制
  }
  //默认字母小写，手动转大写
  return result.toUpperCase() //另toLowerCase()转小写
}
